/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski, Maciej Gajewski            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QMutexLocker>

#include "globaldata.h"

GlobalData* GlobalData::_pInstance = NULL;		// instance declaration

// ========================= constructor ====================
GlobalData::GlobalData()
{
	_paramsNew = false;
	_connectionsNum = 0;
	_addressNew = false;
	_udpHostPort = 0;
}
	
// ========================= instance ====================
/// Provides instance pointer
GlobalData* GlobalData::instance()
{
	if ( ! _pInstance )
	{
		_pInstance = new GlobalData();
	}
	
	return _pInstance;
}

// ========================= setParams ====================
/// Sets params icoming from adjuster
void GlobalData::setParams( const GParams& params )
{
	QMutexLocker l(&_mutex);

	_params = params;
	_paramsNew = true;
}

// ========================= getParams ====================
/// Provides params for main loop
GParams GlobalData::getParams()
{
	QMutexLocker l(&_mutex);
	
	_paramsNew = false;
	return _params;
}

// ========================= incConnections ====================
/// Increases conenction counter
void GlobalData::incConnections()
{
	// i thonk there is no need to lock here
	_connectionsNum ++;
}

// ========================= decConnections ====================
/// Decreases connection counter
void GlobalData::decConnections()
{
	// TODO looks like this is not called
	// i think there is no need to lock here
	if ( _connectionsNum > 0 )
	{
		_connectionsNum --;
	}
	else
	{
		qDebug("GlobalData::decConnections called when coenctions count is zero" );
	}
	
	// reset udp address when all conenctions go down
	if ( _connectionsNum == 0 )
	{
		_udpHostAddress = QHostAddress();
		_udpHostPort = 0;
		_addressNew = true;
	}
}

// ========================= setUpdAddress ====================
/// Sets address incoming from client
void GlobalData::setUpdAddress( const QHostAddress& addr, quint16 port )
{
	QMutexLocker l(&_mutex);
	
	_udpHostPort = port;
	_udpHostAddress = addr;
	_addressNew = true;
}

// ========================= getUdpAddress ====================
/// Gets udp address
QHostAddress GlobalData::getUdpAddress()
{
	QMutexLocker l(&_mutex);
	_addressNew = false;
	
	return 	_udpHostAddress;
}

// eof

